package db;

import Info.SystemEnum;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Brand;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Nicholas
 * @version 1.0
 */
public class ManageBrandDB {
    private List<Brand> brandsList;
    private Log log;

    public ManageBrandDB(Log log) {
        brandsList = DataBase.getInstance().getBrandsList();
        this.log = log;
    }

    public ManageBrandDB(List<Brand> brandsList) {
        this.brandsList = brandsList;
        log = LogFactory.getLog(getClass());
    }

    //查找品牌
    public int find(Brand find){
        int index = 0;
        if(find!=null){
            for(Brand f : brandsList){
                if(f.equals(find)) break;
                index+=1;
            }
            if(index<brandsList.size()){
                log.info(getClass()+"\tfind\t"+SystemEnum.SUCCESS.toString());
                return index;
            }
        }
        log.info(SystemEnum.ERROR_MATCH.toString());
        return -1;
    }

    //查找字段
    public List<Brand> find(String field,String content){
        List<Brand> findByField = new ArrayList<>();
        if(content==null) return findByField;
        try {
            switch (field){
                case "brandNum":
                    for(Brand find : brandsList){
                        if(find.getBrandNum()==Integer.parseInt(content)) findByField.add(find);
                    }
                    break;
                case "brandName":
                    for(Brand find : brandsList){
                        if(find.getBrandName().equals(content)) findByField.add(find);
                    }
                    break;
                case "power":
                    for(Brand find : brandsList){
                        if(find.getRemark().equals(content)) findByField.add(find);
                    }
                    break;
                default:
                    log.info(SystemEnum.ERROR_INPUT.toString());
            }
        }catch (Exception e){
            log.info(SystemEnum.ERROR_INPUT.toString());
            return null;
        }
        return findByField;
    }

    public void printAll(){
        for (Brand brand:brandsList) {
            System.out.println(brand.getBrandNum()+"."+brand.getBrandName());
        }
    }
    //添加品牌
    public boolean add(Brand brand){
        if(brand!=null){
            if(find(brand)==-1){
                brandsList.add(brand);
                log.info(getClass()+"\tadd\t"+SystemEnum.SUCCESS.toString());
                return true;
            }else{
                log.info(SystemEnum.ERROR_REPEAT.toString());
                return false;
            }
        }
        log.info(SystemEnum.ERROR_MATCH.toString());
        return false;
    }

    //删除品牌
    public boolean delete(Brand brand){
        if(brand!=null){
            int index = find(brand);
            if(index == -1){
                log.info(SystemEnum.ERROR_MATCH.toString());
                return false;
            }else{
                brandsList.remove(index);
                log.info(getClass()+"\tdelete\t"+SystemEnum.SUCCESS.toString());
                return true;
            }
        }else{
            log.info(SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }

    public boolean modify(Brand brand,String field,String content){
        if(brand!=null){
            int index = find(brand);
            if(index==-1){
                log.info(SystemEnum.ERROR_MATCH.toString());
                return false;
            }else{
                Brand find = brandsList.get(index);
                try {
                    switch (field) {
                        case "brandNum":
                            find.setBrandNum(Integer.valueOf(content));
                            break;
                        case "brandName":
                            find.setBrandName(content);
                            break;
                        case "remark":
                            find.setRemark(content);
                            break;
                        default:
                            log.info(SystemEnum.ERROR_INPUT.toString());
                    }
                    log.info(getClass()+"\tmodify\t"+SystemEnum.SUCCESS.toString());
                    return true;
                }catch (Exception e){
                    log.info(SystemEnum.ERROR_INPUT.toString());
                    return false;
                }
            }
        }else{
            log.info(SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }

    public int getListSize(){
        return brandsList.size();
    }
}
